//
//  DLTimeSharingView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/12/4.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLTimeSharingView.h"


@interface DLTimeSharingView()

@property (nonatomic, strong) UILabel *noticeLabel;

@end

@implementation DLTimeSharingView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.xValuesDataSource = [NSMutableArray array];
        self.setsDataSource = [NSMutableArray array];
        [self layoutSubControls];
        
        self.backgroundColor = kDLMachineTableAndCellBgColor;
        self.backgroundColor = [UIColor whiteColor];
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(17, 17)];
        
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = maskPath.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask  = maskLayer;
    }
    return self;
}

- (void) chartValueSelected:(ChartViewBase *)chartView entry:(ChartDataEntry *)entry highlight:(ChartHighlight *)highlight {
    NSLog(@"图表中的数值被选中");
    NSInteger index = entry.x;
    NSDictionary *dic = self.dataSourceDic[@"data"];
    NSArray *kDataArray = [NSArray arrayWithArray:dic[@"kData"]];
    NSDictionary *diccc = kDataArray[index];
    
    self.noticeLabel.hidden = NO;
    
    self.noticeLabel.text = [NSString stringWithFormat:@"%@", diccc[@"value"]];
}
- (void)chartTranslated:(ChartViewBase * _Nonnull)chartView dX:(CGFloat)dX dY:(CGFloat)dY {
    NSLog(@"图表被移动");
    NSLog(@"%f--%f", dX, dY);
}
- (void)chartScaled:(ChartViewBase * _Nonnull)chartView scaleX:(CGFloat)scaleX scaleY:(CGFloat)scaleY {
    
    ChartViewPortHandler *scalexxxxX = self.lineChartView.viewPortHandler;
    NSLog(@"缩放了%.2f倍", scalexxxxX.scaleX);
}

- (void) setDataSourceDic:(NSDictionary *)dataSourceDic {
    _dataSourceDic = dataSourceDic;
//    NSLog(@"折线图的数据源是：%@", dataSourceDic);
    
    [self.xValuesDataSource removeAllObjects];
    [self.setsDataSource removeAllObjects];
    NSDictionary *dic = dataSourceDic[@"data"];
    
    self.titleLabel.text = [NSString stringWithFormat:@"%@", dic[@"pTitle"]];
    
    NSArray *kDataArray = [NSArray arrayWithArray:dic[@"kData"]];
    if (kDataArray.count > 0) {
        
        for (NSInteger i = 0; i < kDataArray.count; i++) {
            NSDictionary *dataDic = kDataArray[i];
            NSString *xTitleStr = [NSString stringWithFormat:@"%@", dataDic[@"item"]];
            NSString *newTitleStr = [xTitleStr stringByReplacingOccurrencesOfString:@" " withString:@"\n"];
            NSString *setsValueStr = [NSString stringWithFormat:@"%@", dataDic[@"value"]];
            
            [self.xValuesDataSource addObject:newTitleStr];
            [self.setsDataSource addObject:setsValueStr];
        }
    }
    ChartIndexAxisValueFormatter *formatter = [[ChartIndexAxisValueFormatter alloc] initWithValues:self.xValuesDataSource];
    [self.lineChartView.xAxis setValueFormatter:formatter];
    
    self.lineChartView.rightAxis.enabled = NO;
    
//    [self.lineChartView animateWithXAxisDuration:0.3];
    
    [self configSetData:self.xValuesDataSource.count];
}

- (void)configSetData:(NSInteger)count {
    
    NSMutableArray *mutValues = [[NSMutableArray alloc] init];
    NSMutableArray *numberMutArr = [NSMutableArray array];
    for (NSInteger i = 0; i < count; i++) {
        NSString *valueStr = self.setsDataSource[i];
        double val =  valueStr.doubleValue;
        [mutValues addObject:[[ChartDataEntry alloc] initWithX:i y:val icon: [UIImage imageNamed:@"noname,theredon'tneedanimage"]]];
        
        NSNumber *numberValue = [NSNumber numberWithDouble:val];
        [numberMutArr addObject:numberValue];
    }
    
    CGFloat maxValue = [[numberMutArr valueForKeyPath:@"@max.floatValue"] floatValue];
    CGFloat minValue = [[numberMutArr valueForKeyPath:@"@min.floatValue"] floatValue];
//    NSLog(@"最大值：%.2f", maxValue);
//    NSLog(@"最小值：%.2f", minValue);
    self.leftAxis.axisMaximum = maxValue;
    self.leftAxis.axisMinimum = minValue;
    
    
    LineChartDataSet *set1 = nil;
    if (_lineChartView.data.dataSetCount > 0) {
        set1 = (LineChartDataSet *)_lineChartView.data.dataSets[0];
        set1.values = mutValues;
        [_lineChartView.data notifyDataChanged];
        [_lineChartView notifyDataSetChanged];
    } else {
        
        set1 = [[LineChartDataSet alloc] initWithValues:mutValues label:@""];
        [set1 setColor:kDLNormalBlackColor];
        set1.drawCirclesEnabled = NO;
        set1.mode = LineChartModeCubicBezier;
        set1.circleHoleRadius = 1;
        set1.circleRadius = 1;
        set1.lineWidth = 1;
        set1.drawCircleHoleEnabled = NO;
        set1.drawValuesEnabled = YES;
        //折线转折点的文字颜色
        set1.valueColors = @[kDLNormalBlackColor];
        set1.highlightEnabled = YES;
        set1.highlightColor = [UIColor orangeColor];
        //是否填充封闭区域以及封闭颜色
//        set1.drawFilledEnabled = YES;
//        [set1 setFillColor:[UIColor greenColor]];
        
        
//        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
//        [formatter setPositiveFormat:@"#0.00"];
//        self.lineChartView.leftAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc] initWithFormatter:formatter];
        
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
        _lineChartView.data = data;
    }
}
- (void) layoutSubControls {
    
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(12, 10, self.width - 24, 20);
    titleLabel.textColor = kDLNormalBlackColor;
    titleLabel.font = kBoldFont(14);
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    
    LineChartView *lineChartView = [[LineChartView alloc] initWithFrame:CGRectMake(kMachineChartsChartLeftMargin, 0, self.width - kMachineChartsChartLeftMargin *2, kMachineChartsHeight - 40)];
    [lineChartView setExtraOffsetsWithLeft:0 top:40 right:0 bottom:0];
    lineChartView.delegate = self;
    [self addSubview:lineChartView];
    self.lineChartView = lineChartView;
    ChartViewPortHandler *scaleX = self.lineChartView.viewPortHandler;
    [scaleX setMinimumScaleX:1.0];
    [scaleX setMaximumScaleX:30.0];
    lineChartView.backgroundColor = kDLMachineTableAndCellBgColor;
    lineChartView.backgroundColor = [UIColor whiteColor];
    
    lineChartView.noDataText = @"暂无数据";
    lineChartView.autoScaleMinMaxEnabled = NO;
    lineChartView.gridBackgroundColor = kDLMachineTableAndCellBgColor;
    lineChartView.gridBackgroundColor = [UIColor whiteColor];
    lineChartView.pinchZoomEnabled = YES;
    lineChartView.borderColor = [UIColor grayColor];
    lineChartView.borderLineWidth = 0.5;
    lineChartView.drawGridBackgroundEnabled = YES;
    lineChartView.drawBordersEnabled = YES;
    lineChartView.minOffset = 10;
    lineChartView.keepPositionOnRotation = YES;
    lineChartView.maxVisibleCount = 10;
    lineChartView.legend.enabled = NO;
    lineChartView.scaleYEnabled = NO;
    
    ChartXAxis *xAxis = lineChartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.drawGridLinesEnabled = YES;
    //    xAxis.granularityEnabled = YES;
    xAxis.labelCount = 4;
    xAxis.forceLabelsEnabled = NO;
    xAxis.labelTextColor = kDLNormalBlackColor;
    xAxis.axisLineWidth = 0.5;
    xAxis.axisLineColor = [UIColor grayColor];
    xAxis.gridColor = [UIColor grayColor];
    
    
    ChartYAxis *leftAxis = lineChartView.leftAxis;
//    leftAxis.axisMinimum = 10.4;
//    leftAxis.axisMaximum = 10.7;
    leftAxis.labelCount = 6;
    leftAxis.drawGridLinesEnabled = YES;
    leftAxis.forceLabelsEnabled = NO;
    leftAxis.axisLineWidth = 0.5;
    leftAxis.axisLineColor = [UIColor grayColor];
    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
    leftAxis.labelTextColor = kDLNormalBlackColor;
    leftAxis.labelFont = kSystemFont(12);
    
    leftAxis.inverted = NO;
    leftAxis.zeroLineWidth = 0.5;
    leftAxis.drawZeroLineEnabled = YES;
    leftAxis.gridColor = [UIColor grayColor];
    self.leftAxis = leftAxis;
    [self bringSubviewToFront:titleLabel];
    
    UILabel *noticeLabel = [UILabel new];
    noticeLabel.frame = CGRectMake(self.width - self.width / 3 - kMachineChartsChartLeftMargin, 10, self.width / 3, 20);
    noticeLabel.textColor = kDLNormalBlackColor;
    noticeLabel.backgroundColor = kDLMachineTableAndCellBgColor;
    noticeLabel.backgroundColor = [UIColor whiteColor];
    noticeLabel.textAlignment = NSTextAlignmentCenter;
    noticeLabel.font = kSystemFont(14);
    noticeLabel.numberOfLines = 0;
    noticeLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [self addSubview:noticeLabel];
    self.noticeLabel = noticeLabel;
    noticeLabel.hidden = YES;
    
    //设置y轴0.xx的时候显示0.xx而不是.xx
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setPositiveFormat:@"#0.00"];
    self.lineChartView.leftAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc] initWithFormatter:formatter];
    
    self.lineChartView.drawMarkers = YES;
    ChartMarkerView * makerView = [[ChartMarkerView alloc]init];
    makerView.offset = CGPointMake(-50,-50);
    makerView.chartView = _lineChartView;
    self.lineChartView.marker = makerView;
    
//    [makerView addSubview:self.subPriceView];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
